Android TV Apps Development: Building for Media and Games by Paul Trebilcox-Ruiz
Author:Paul Trebilcox-Ruiz [Trebilcox-Ruiz, Paul]
Language: eng
Format: epub, pdf
Tags: Computers, Programming, Mobile Devices, General, Interactive & Multimedia, Networking, Design; Graphics & Media, Graphics Tools
ISBN: 9781484217849
Google: ba5PCwAAQBAJ
Publisher: Apress
Published: 2015-12-30T20:23:42.827104+00:00
public static final int SPEECH_REQUEST_CODE = 42;
private ArrayObjectAdapter mRowsAdapter;
private List<Video> mVideos;
At the end of onCreate you will call loadData() and initialize mRowsAdapter with a new ListRowPresenter.
loadData();
mRowsAdapter = new ArrayObjectAdapter( new ListRowPresenter() );
loadData will take the content JSON data and create a list of video objects, and should look familiar to you since you have implemented this method before in other classes for this project.
private void loadData() {
String json = Utils.loadJSONFromResource(getActivity(), R.raw.videos);
Type collection = new TypeToken<ArrayList<Video>>(){}.getType();
Gson gson = new Gson();
mVideos = gson.fromJson(json, collection);
}
Now that you have the data prepared, itâs time to start searching. For this example you will use a method named loadQuery that will accept a string from your users and find content from the data that matches the query. This method will first clear the results of previous searches and validate that the query exists and is not empty. Next you will loop through the data and see if the media titles contain the query provided by the users. While this is not the most efficient way to retrieve results from the data, it is a simple and easy-to-understand approach while learning how Android TV displays search results. Each item that matches the search query will be placed into a new ArrayObjectAdapter to be displayed as cards. Once the ArrayObjectAdapter is populated with the results, a new Header and ListRow will be added to the top-level ArrayObjectAdapter to display the results.
private void loadQuery(String query) {
if(mRowsAdapter != null)
mRowsAdapter.clear();
if(query == null || query.length() == 0)
return;
ArrayObjectAdapter listRowAdapter = new
ArrayObjectAdapter(new CardPresenter());
for(Video video : mVideos) {
if(video.getTitle() != null &&
video.getTitle().toLowerCase().contains(query.toLowerCase())) {
listRowAdapter.add(video);
}
}
if(listRowAdapter.size() == 0)
return;
HeaderItem header = new HeaderItem("Results");
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
When loadQuery is complete, you will need to wire it up to MediaSearchFragment so that the application knows to call that method. You have three stub methods from the SearchResultProvider interface that you will need to use. getResultsAdapter will simply return mRowsAdapter so that the fragment will know what will be displayed. onQueryTextChange and onQueryTextSubmit will each take the query string argument and then call loadQuery. You will notice that both of these methods also return a Boolean. If you return true, it means that the results have changed due to the query. For this simple demonstration we will always return true, even though the results may not be different from the last time one of these methods was called, in order to keep things simple.
@Override
public ObjectAdapter getResultsAdapter() {
return mRowsAdapter;
}
@Override
public boolean onQueryTextChange(String newQuery) {
loadQuery(newQuery);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
loadQuery(query);
return true;
}
At this point you will be able to run your application and type in your search query to return results, as seen in Figure 4-3.
Figure 4-3.In-app search screen implementation
Download
Android TV Apps Development: Building for Media and Games by Paul Trebilcox-Ruiz.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25281)
Hello! Python by Anthony Briggs(24331)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23419)
Kotlin in Action by Dmitry Jemerov(22501)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21955)
Dependency Injection in .NET by Mark Seemann(21836)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20701)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19514)
Grails in Action by Glen Smith Peter Ledbrook(18593)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17028)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13683)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11847)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11149)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10621)
Hit Refresh by Satya Nadella(9185)
The Kubernetes Operator Framework Book by Michael Dame(8560)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8400)
Robo-Advisor with Python by Aki Ranin(8356)